// _Test2_OpenVideo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	// create window
	cvNamedWindow("Example2",CV_WINDOW_AUTOSIZE);

	// open a file video "MotherEarth.avi" path "D:\\MotherEarth.avi"
	CvCapture* capture= cvCreateFileCapture("D:\\MotherEarth.avi");

	// create a frame ( an image )
	IplImage* frame;

	// time for process ( timeProcess = currentTime - previousTime )
	int previousTime, time;


	// get the frame-per-seconds (FPS) of the video, ( know the speed = wait time )
	int fps = cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);


	// while video not end, read frame and show it to Windows example
	// at the beginning, the Pointer point to first frame
	while(1)
	{
		// read current frame and point to next frame
		frame = cvQueryFrame(capture);	

		// get time for calculate processTime
		previousTime = clock();			

		if(!frame)
			break;

		//////////////////////////////////////////////////////////////////////////
		//	Processing image here

		// show it
		cvShowImage("Example2",frame);

		//////////////////////////////////////////////////////////////////////////


		time = fps - (clock() - previousTime);
		if(time < 1)
			time = 1;

		// get key, wait for second
		printf("%d %d",time,fps);
		char c = cvWaitKey(time);

		// if input is ESC, exit the loop
		if(c == 27)						
			break;
	}

	// Deallocate thing
	cvReleaseCapture(&capture);
	cvDestroyWindow("Example2");

	return 0;
}

